<script lang="ts" setup>
import type { PerEmployeeApi } from '#/api/per/employee';

import { ref } from 'vue';

import { Page } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';
import { handleTree } from '@vben/utils';

import {
  Card,
  RadioButton,
  RadioGroup,
  Result,
  Space,
  Spin,
  Tag,
} from 'ant-design-vue';

import { getEmployeeByLogin } from '#/api/per/employee';
import { getOperateCategoriesSimpleList } from '#/api/system/operateCategories';
import { Image } from '#/components/image';

import FunctionCategory from './components/functionCategory.vue';

const selectedCategory = ref(100);
// 添加加载状态
const loading = ref(false);
const desensitize = ref<boolean>(true); // 是否脱敏
function handleDesensitize() {
  desensitize.value = !desensitize.value;
  getUserInfo();
}
// 获取用户基本情况信息
const employeeInfo = ref<PerEmployeeApi.Employee>({});
const operateList = ref<any[]>([]);
const tabList = ref<any[]>([]);
const operateAllList = ref<any[]>([]);
const showOperateList = ref<any[]>([]);
const operateInfo = ref<any>({});
async function getUserInfo() {
  loading.value = true;
  const data = await getEmployeeByLogin(desensitize.value);
  employeeInfo.value = data;
  loading.value = false;
}
getUserInfo();

async function getOperateList() {
  const data = await getOperateCategoriesSimpleList({ codePath: 'RYDA-' });
  operateList.value = handleTree(data);
  if (operateList.value.length > 0) {
    operateInfo.value = operateList.value[0];
    // 获取所有的功能，递归判断所有子集,判断是否有子项，有子项就不加入alllist，没有子项就加入
    const allList: any[] = [];
    const allTabList: any[] = [];
    function getChildren(item: any) {
      if (item.children?.length > 0) {
        item.children.forEach((child) => {
          getChildren(child);
        });
        allTabList.push(item);
      } else {
        allList.push(item);
      }
    }
    operateInfo.value.children.forEach((item) => {
      getChildren(item);
    });
    operateAllList.value = allList;
    tabList.value = allTabList;
    showOperateList.value = operateAllList.value;
  }
}

getOperateList();
function handleCategoryChange(e) {
  console.log(e.target.value);
  if (e.target.value === 100) {
    showOperateList.value = operateAllList.value;
  } else {
    const allList: any[] = [];
    function getTabChildren(item: any) {
      if (item.children?.length > 0) {
        item.children.forEach((child) => {
          getTabChildren(child);
        });
      } else {
        allList.push(item);
      }
    }
    tabList.value
      .find((item) => item.id === e.target.value)
      ?.children.forEach((item) => {
        getTabChildren(item);
      });
    showOperateList.value = allList;
  }
}
</script>

<template>
  <Page auto-content-height>
    <div v-if="employeeInfo?.id">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-overlay">
        <Spin size="large" tip="加载中..." />
      </div>
      <Space size="middle" direction="vertical" style="width: 100%">
        <!-- 个人基本信息展示区 -->
        <div class="animate-fade-in">
          <Card class="user-basic-info" hoverable>
            <div class="user-info-container">
              <div class="avatar-section">
                <div class="avatar-wrapper">
                  <div class="avatar-container">
                    <Image
                      v-if="employeeInfo?.photoUrl"
                      width="120px"
                      height="120px"
                      alt="个人照片"
                      :src="employeeInfo?.photoUrl"
                    />
                    <div v-else class="avatar-placeholder">
                      {{ employeeInfo?.fullName?.charAt(0) || '?' }}
                    </div>
                  </div>

                  <div class="avatar-badge">
                    <Tag color="success" class="online-status">正常</Tag>
                  </div>
                </div>
                <div class="user-name-title">
                  <h3 class="user-name">{{ employeeInfo?.fullName }}</h3>
                  <p class="user-job-title">
                    {{ employeeInfo?.teachingSubject }}
                  </p>
                </div>
              </div>
              <div class="basic-info-section">
                <div class="info-grid">
                  <div class="info-item">
                    <span class="info-label">性别</span>
                    <span class="info-value">{{
                      employeeInfo?.sex === 1 ? '男' : '女'
                    }}</span>
                  </div>
                  <div class="info-item">
                    <span class="info-label">年龄</span>
                    <span class="info-value">{{ employeeInfo?.age }}岁</span>
                  </div>
                  <div class="info-item">
                    <span class="info-label">手机号</span>
                    <span class="info-value">{{
                      employeeInfo?.mobile || '-'
                    }}</span>
                  </div>
                  <div class="info-item">
                    <span class="info-label">所属单位</span>
                    <span class="info-value">{{
                      employeeInfo?.unitName || '-'
                    }}</span>
                  </div>
                  <div class="info-item">
                    <span class="info-label">所属部门</span>
                    <span class="info-value">{{
                      employeeInfo?.departmentName || '-'
                    }}</span>
                  </div>
                  <div class="info-item">
                    <span class="info-label">岗位</span>
                    <span class="info-value">{{
                      employeeInfo?.postName || '-'
                    }}</span>
                  </div>
                </div>
              </div>
            </div>
          </Card>
        </div>
        <!-- 功能分类区域 -->
        <Card hoverable class="user-basic-info">
          <template #title>
            <div class="category-header">
              <IconifyIcon :icon="operateInfo.icon" class="category-icon" />
              <span class="category-title">功能导航</span>
              <div style="margin-left: 20px">
                <RadioGroup
                  v-model:value="selectedCategory"
                  button-style="solid"
                  @change="handleCategoryChange"
                >
                  <RadioButton :value="100">全部</RadioButton>
                  <RadioButton
                    v-for="item in tabList"
                    :key="item.id"
                    :value="item.id"
                  >
                    {{ item.name }}
                  </RadioButton>
                </RadioGroup>
              </div>
            </div>
          </template>
          <div>
            <FunctionCategory :items="showOperateList" />
          </div>
        </Card>
      </Space>
    </div>
    <Card v-else>
      <Result
        status="warning"
        title="您的账号没有权限!"
        sub-title="请先联系单位管理员添加基本信息。"
      />
    </Card>
  </Page>
</template>

<style lang="less" scoped>
.user-basic-info {
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(
    135deg,
    hsl(var(--card)) 0%,
    hsl(var(--background-deep)) 100%
  );
  border: 1px solid hsl(var(--border));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  }
}

.user-info-container {
  display: flex;
  align-items: flex-start;
  padding: 12px 24px;
}

.avatar-section {
  margin-right: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.avatar-wrapper {
  position: relative;
  margin-bottom: 12px;
}

.avatar-placeholder {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  font-weight: bold;
  border: 4px solid hsl(var(--card));
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
  cursor: default;
  user-select: none;

  .user-basic-info:hover & {
    transform: scale(1.05);
  }
}

.avatar-badge {
  position: absolute;
  bottom: 0;
  right: 0;
  background: white;
  border-radius: 12px;
  padding: 2px 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.online-status {
  font-size: 12px;
  padding: 2px 8px;
}

.user-name-title {
  text-align: center;
}

.user-name {
  margin: 0 0 4px 0;
  font-size: 20px;
  font-weight: 600;
  color: hsl(var(--foreground));
}

.user-job-title {
  margin: 0;
  font-size: 14px;
  color: hsl(var(--muted-foreground));
}

.basic-info-section {
  flex: 1;
  padding: 16px 0;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.info-item {
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  background: hsl(var(--card));
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transform: translateY(-1px);
  }
}

.info-label {
  font-size: 12px;
  color: hsl(var(--muted-foreground));
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-value {
  font-size: 15px;
  font-weight: 500;
  color: hsl(var(--foreground));
}

.category-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  padding: 0;
}
.category-title {
  font-size: 16px;
  color: hsl(var(--foreground));
  transition: color 0.3s ease;
}
.category-icon {
  font-size: 20px;
  margin-right: 8px;
  color: hsl(var(--primary));
  transition: transform 0.3s ease;
}
@media (max-width: 1024px) {
  .info-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .user-info-container {
    flex-direction: column;
    padding: 16px;
    text-align: center;
  }

  .avatar-section {
    margin-right: 0;
    margin-bottom: 20px;
  }

  .info-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}

/* 加载遮罩层 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: hsl(var(--overlay));
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

/* 动画效果 */
.animate-fade-in {
  animation: fadeInUp 0.5s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
